package fr.pud.client.controller;

import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import fr.pud.client.i18n.LanguagesConstants;
import fr.pud.client.images.ImagesDatabase;
import fr.pud.client.view.MainFrame;
import fr.pud.utils.Tools;
import fr.pud.utils.Tools4PUDStation;

/**
 * The controller to use to close the program.
 * Apr 9, 2012 - OSE.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         CloseProgram.java
 */
public class CloseProgram extends AbstractAction {
    /**
     * The serial UID
     */
    private static final long serialVersionUID = 8441366964148080954L;

    /**
     * The default constructor which sets the controller up.
     */
    public CloseProgram() {
        this.putValue(NAME,
                LanguagesConstants.LANGUAGE.getObject(LanguagesConstants.QUIT));
        this.putValue(SMALL_ICON, ImagesDatabase.RED_CROSS);
        this.putValue(ACCELERATOR_KEY,
                KeyStroke.getKeyStroke(KeyEvent.VK_Q, InputEvent.CTRL_MASK));
    }

    /**
     * Method to close the program
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        if (JOptionPane.showConfirmDialog(MainFrame.getMainFrame(),
                LanguagesConstants.LANGUAGE.getString(LanguagesConstants.CLOSE)
                        + " ?") == 0) {
            Tools4PUDStation.serializePUDStationsDatabase();
            System.exit(0);
            try {
                Tools.LOG_FILE_ERRORS.close();
            }
            catch (IOException e1) {
                JOptionPane.showMessageDialog(null, LanguagesConstants.LANGUAGE
                        .getString(LanguagesConstants.ERROR_CLOSE_FILE),
                        LanguagesConstants.LANGUAGE
                                .getString(LanguagesConstants.ERROR_OCCURED),
                        JOptionPane.ERROR_MESSAGE, ImagesDatabase.RED_CROSS);
            }
        }
    }
}
